package org.drushpal  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.registerClassAlias;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	
	import org.drushpal.model.ConnectionModel;
	import org.drushpal.model.RepositoryModel;
	import org.drushpal.model.enum.Icons;
	import org.drushpal.model.vo.Connection;
	import org.drushpal.model.vo.Project;
	import org.drushpal.model.vo.Release;
	import org.drushpal.model.vo.Term;
	import org.drushpal.service.CloseConnectionEvent;
	import org.drushpal.view.ControlPanel;
	import org.drushpal.view.forms.ConnectionEvent;
	import org.drushpal.view.forms.ConnectionForm;
	import org.drushpal.view.forms.SettingsForm;
	import org.robotlegs.mvcs.Mediator;
	
	public class DrushPalMediator extends Mediator
	{
		[Inject]public var drushPal:DrushPal;
		
		[Inject]public var repositoryModel:RepositoryModel;
		[Inject]public var connectionModel:ConnectionModel;
		
		public function DrushPalMediator()
		{
		}
		
		override public function onRegister():void{			
			eventMap.mapListener(drushPal,ConnectionEvent.ADD_CONNECTION,onAddConnection);
			eventMap.mapListener(drushPal,ConnectionEvent.EDIT_CONNECTION,onEditConnection);
			eventMap.mapListener(drushPal,ConnectionEvent.REMOVE_CONNECTION,onRemoveConnection);
			eventMap.mapListener(drushPal,ConnectionEvent.LAUNCH_CONNECTION,onLaunchConnection);
			eventMap.mapListener(eventDispatcher,CloseConnectionEvent.CLOSE,onCloseConnection);
			eventMap.mapListener(drushPal.btnOpenSettings,MouseEvent.CLICK,onOpenSettings);
			drushPal.connections = connectionModel.connections;
			//drushPal.maximize();
		}
		private function onOpenSettings(event:MouseEvent):void{
			eventDispatcher.dispatchEvent(new DrushPalEvent(DrushPalEvent.OPEN_SETTINGS));
		}
		
		private function onRemoveConnection(event:ConnectionEvent):void{
			var connection:Connection = event.connection;
			Alert.show("Are you sure you want to delete this connection? This can not be undone.","Warning",Alert.YES|Alert.NO,contextView as Sprite,
				function(event:CloseEvent):void{
					if(event.detail == Alert.YES) connectionModel.removeConnection(connection);
				},Icons.WARNING);
			
		}
		private function onCloseConnection(event:CloseConnectionEvent):void{
			drushPal.currentState='normal';
			drushPal.drushArea.removeAllElements();
		}
		
		private function onLaunchConnection(event:ConnectionEvent):void{
			drushPal.currentState='connected';
			var controlPanel:ControlPanel = new ControlPanel();
			drushPal.drushArea.addElement(controlPanel);
			controlPanel.connection = event.connection;
		}
		private function onEditConnection(event:ConnectionEvent):void{
			var	connectionForm:ConnectionForm = new ConnectionForm();
			PopUpManager.addPopUp(connectionForm,contextView,true);
			PopUpManager.centerPopUp(connectionForm);
			connectionForm.connection = event.connection;
			mediatorMap.createMediator(connectionForm);
		}
		private function onAddConnection(event:ConnectionEvent):void{				
			var	connectionForm:ConnectionForm = new ConnectionForm();
			PopUpManager.addPopUp(connectionForm,contextView,true);
			PopUpManager.centerPopUp(connectionForm);
			connectionForm.connection = new Connection();
			mediatorMap.createMediator(connectionForm);
		}
	}
}